package admin

// by daoyi
//文件上传示例
import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"net/http"
	"os"
	"path"
	"simai_oa/controller"
	"simai_oa/model"
	"simai_oa/tool"
	"strconv"
	"strings"
	"time"
)

type Upload struct{}

func (Upload) Index(c *gin.Context) {
	username, _ := c.Get("username")
	fmt.Println(username)
	c.HTML(http.StatusOK, "admin/upload.html", gin.H{
		"title": "首页sss111",
	})
}

// DoUpload
//
//		@Summary	单文件上传
//		@Tags		后台-项目订单
//	 	@param		face	formData	file	false	"文件"
//		@Success	200		{string}	json{"code","message","data"}
//		@Router		/admin/upload/doUpload [POST]
func (Upload) DoUpload(c *gin.Context) {

	file, err := c.FormFile("face")
	if err == nil {
		//获取图像后缀
		y := path.Ext(file.Filename)
		allowExtMap := map[string]bool{
			".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".svg": true, ".txt": true, ".pdf": true, ".doc": true,
			".ppt": true, ".xls": true, ".rar": true, ".zip": true, ".7z": true, ".mp4": true, ".mp3": true, ".wav": true,
			".mov": true, ".wmv": true, ".flv": true, ".3gp": true, ".mkv": true, ".vob": true, ".ape": true, ".rm": true, ".rmvb": true,
			".aac": true, ".flac": true, ".wma": true, ".m4a": true, ".mp2": true, ".m4p": true, ".m4b": true, ".aif": true,
			".cda": true, ".mid": true, ".midi": true, ".aifc": true, ".aiff": true, ".docx": true, ".xlsx": true, ".pptx": true,
			".mpg": true, ".asf": true, ".svc": true, ".aes": true, ".mpga": true, ".xlsm": true, ".xlsb": true,
			".xlw": true, ".xla": true, ".xlc": true, ".xld": true, ".xlm": true, ".pct": true, ".dwg": true, ".pic": true,
			".prn": true, ".sda": true, ".mdb": true, ".vcf": true, ".eml": true, ".msg": true, ".wml": true, ".pr": true,
			".pst": true, ".wks": true, ".wps": true, ".wtx": true, ".wkss": true, ".wpsp": true, ".wpt": true, ".arj": true,
		}
		if _, ok := allowExtMap[y]; !ok {
			c.JSON(http.StatusOK, gin.H{
				"code": 2,
				"msg":  "请上传报销材料证明文件,上传的文件是常用的视频、图片、文件和压缩文件如：jpeg,jpg,png,gif,docx,doc,xlsx,xlsm,xlm,cvs,7z,zip,txt,rar,mp4,wmv,mov,mkv,pdf......",
			})
			return
		}
		//获取时间戳
		a := time.Now().Unix()
		//获取一个1W以内的随机数
		b := rand.Intn(10000)
		//将时间戳的类型转换
		z := strconv.FormatInt(a, 10)
		//将随机书转换类型
		x := strconv.FormatInt(int64(b), 10)
		mkdirpath := tool.CreateDateDir("./static/upload")
		//写入保持位置与自定义名称，并且带上文件自带的后缀
		dst := path.Join(mkdirpath, z+x+y)
		//保存文件到相应位置
		c.SaveUploadedFile(file, dst)

		//图片地址dst中含有"\\",将地址中的"\\"替换后写入数据库
		dst = strings.Replace(dst, "\\", "/", -1)

		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"msg":  "ok",
			"path": dst,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 2,
			"msg":  "文件上传错误",
		})
		return
	}

}

// 多个文件，不同名接收方式
func (Upload) DuoUp(c *gin.Context) {
	username := c.PostForm("username")
	file, err := c.FormFile("face1")
	file2, err2 := c.FormFile("face2")

	if err == nil && err2 == nil {
		log.Println(err)
		log.Println(err2)
		//file.Filename 获取文件名称
		//path.Join(path.Dir(file.Name), file.Name)拼接路径
		dst := path.Join("./static/upload", file.Filename)
		dst2 := path.Join("./static/upload", file2.Filename)
		//保存文件
		c.SaveUploadedFile(file, dst)
		c.SaveUploadedFile(file, dst2)

		c.JSON(http.StatusOK, gin.H{
			"success":  1,
			"username": username,
			"path":     dst,
			"path2":    dst2,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"success":  2,
			"username": username,
		})
	}
}

// 多文件上传
func (Upload) DuoUpName(c *gin.Context) {

	username := c.PostForm("username")

	//上传图片，图片接收名：face[],table表名,fileType上传的文件类型：1图片、2文件、3视频，id数据所属id
	path1 := tool.Upload(c)
	files := model.File{}
	err := files.CreateFile(1, 2, path1, "admin")

	if err != nil {
		log.Println(err)
		c.JSON(http.StatusOK, gin.H{
			"code": 2,
			"msg":  err,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     1,
		"username": username,
		"path":     path1,
	})

}

// DeleteFile
//
//		@Summary	删除文件
//		@Tags		后台-项目订单
//	 	@param		face	query	string	false	"文件地址"
//		@Success	200		{string}	json{"code","message","data"}
//		@Router		/admin/upload/deleteFile [GET]
func (Upload) DeleteFile(c *gin.Context) {
	face, status := c.GetQuery("face")
	if !status {
		log.Println(status)
		controller.ResSuccess(c, gin.H{"code": 1, "msg": "未获取到要删除的文件地址！请检查！"})
		return
	}
	fmt.Println("===========================================", face)
	// 判断文件是否存在
	if _, err := os.Stat(face); os.IsNotExist(err) {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}

	// 删除文件
	err := os.Remove(face)
	if err != nil {
		log.Println(err)
		controller.ResErrSrv(c, err)
		return
	}

	err1 := tool.Gdb.Table("za_file").Where("src = ?", face).Delete(model.File{}).Error
	if err1 != nil {
		log.Println(err1)
		controller.ResErrSrv(c, err1)
		return
	}

	controller.ResSuccess(c, gin.H{"code": 1, "msg": "文件删除成功！"})
}
